/*

This file is part of the replication package of "Asset-Price Redistribution" by
Fagereng, Gomez, Gouin-Bonenfant, Holm, Moll, and Natvik. 

This file produces a set of statistics on welfare based on the complete
population and their individual welfare meausures. 

*/

global time 1994
global lastyear 2019

** Load savings data **
use b_year *_total lnr year *_pb ltv_debt debt* housing* R0t_* *_shock *_wealth using $data/apr_9319 if year <= $lastyear, clear
tsset lnr year
gen l_total_wealth = l.total_wealth

**
* produce csv file with mean ltv per year
cap drop ltv
cap drop ltv_debt

gen ltv = (debt + debt_pb) / (housing + housing_pb)
qui replace ltv = 0 if ltv == .
qui replace ltv = 1 if ltv > 1
gen ltv_debt = ltv * (debt + debt_pb)

* get sum stats for ltv
cap drop _temp
egen ltvMean = wmean(ltv), w(housing_total) by(year)
bys year: gen _temp = _n
outsheet year ltvMean if _temp == 1 using ${output}/year_mean_ltv.csv, c replace
cap drop _temp
**



** WELFARE BY COHORT FOR DIFFERENT XI VALUES ON THE COLLATERAL CONSTRAINT
forval xival = 0(0.005)0.01 {
preserve

* gen_welfare.ado needed to run here -- is in $programs
cd $programs
gen_welfare, by(lnr) xi(`xival')

collapse (sum) welfare*, by(b_year)

local xitype = `xival'*1000

outsheet b_year welfare_* using $output/welfare_cohort_xi_`xitype'.csv, c replace
restore
} 






forval xival = 0(0.005)0.01 {
preserve
* gen_welfare.ado needed to run here -- is in $programs
gen_welfare, by(lnr) xi(`xival')

tsset lnr year

foreach var in equity housing total debt deposits {
	gen welfare_`var'_sc = 100*(welfare_`var' / l_total_wealth)
}

foreach var in equity housing total {
	gen revgain_`var'_sc = 100*(revgain_`var' / l_total_wealth)
}

local xitype = `xival'*1000

if `xitype' == 0 {
	save $data/distribution_${time}_${lastyear}, replace
} 
else {
	save $data/distribution_${time}_${lastyear}_xi_`xitype', replace
}

restore
} 



********************************************************************************
* SUMMARY STATS
********************************************************************************


forval xival = 5(5)10 {
u lnr year welfare_total using $data/distribution_${time}_${lastyear}, replace
rename welfare_total welfare_total_base
merge 1:1 lnr year using $data/distribution_${time}_${lastyear}_xi_`xival', nogen keep(match master) keepusing(welfare_total)

gen welfare_total_diff = welfare_total - welfare_total_base

preserve
twoway__histogram_gen welfare_total_diff if welfare_total_diff >= -50 & welfare_total_diff < 50, start(-50) w(0.1) fraction gen(y x) 
keep x y
keep if y != .
outsheet x y using ${output}/hist_welfare_total_diff_xi_`xival'_${time}.csv, c replace
restore


global namelist welfare_total_diff
putexcel set ${output}/sum_stats_welfare_total_diff_xi_`xival'_${time}.xlsx, replace

putexcel A1 = "variable" 
putexcel B1 = "mean" 
putexcel C1 = "p0-1" 
putexcel D1 = "p1-10" 
putexcel E1 = "p10-50" 
putexcel F1 = "p50-90" 
putexcel G1 = "p90-99"
putexcel H1 = "p99-100"



local j = 2
foreach var in $namelist {

matrix res = J(1,7,.)

cap drop _temp
sort `var'
gen _temp = floor((_n-1)/_N*100)+1


qui su `var', d
matrix res[1,1] = r(mean)

qui su `var' if _temp <= 1, d 
matrix res[1,2] = r(mean)

qui su `var' if _temp > 1 & _temp <= 10, d 
matrix res[1,3] = r(mean)

qui su `var' if _temp > 10 & _temp <= 50, d 
matrix res[1,4] = r(mean)

qui su `var' if _temp > 50 & _temp <= 90, d 
matrix res[1,5] = r(mean)

qui su `var' if _temp > 90 & _temp <= 99, d 
matrix res[1,6] = r(mean)

qui su `var' if _temp > 99, d 
matrix res[1,7] = r(mean)


putexcel A`j' = "`var'"
putexcel B`j' = matrix(res)
local j = `j' + 1

}

* save as csv
clear
import excel ${output}/sum_stats_welfare_total_diff_xi_`xival'_${time}.xlsx, sheet("Sheet1") firstrow clear
export delimited ${output}/sum_stats_welfare_total_diff_xi_`xival'_${time}.csv, replace
erase ${output}/sum_stats_welfare_total_diff_xi_`xival'_${time}.xlsx
}



********************************************************************************
* SUMMARY STATS
********************************************************************************

u $data/distribution_${time}_${lastyear}, replace

qui estpost summarize welfare_total welfare_housing welfare_equity welfare_debt welfare_deposits, d
esttab using ${output}/sum_stats_welfare_${time}.csv, cells("mean sd p1 p5 p10 p25 p50 p75 p90 p95 p99 count") csv replace noobs plain compress nomtitles


********************************************************************************
* PRODUCE OUTPUT
********************************************************************************


* aggregate welfare
u $data/distribution_${time}_${lastyear}, replace

global NUM = 200
matrix res = J($NUM+1,1,0)
matrix Gamma = J($NUM+1,1,0)


* winsorize weights at bottom 5% of total_wealth
cap drop temp
gen temp = total_wealth
qui su temp, d
replace temp = r(p5) if temp < r(p5)
	

forval j = 0/$NUM {
	di (`j'/($NUM/20))
	cap drop weight
	cap drop planner_welfare

	* compute weights
	gen weight = temp^(-`j'/($NUM/20))
	
	* normalize weights
	cap drop sumW
	egen sumW = sum(weight)
	qui replace weight = weight/sumW
	drop sumW
	
	egen planner_welfare = sum(weight*welfare_total)
	qui su planner_welfare
	matrix res[`j'+1,1] = r(mean)
	matrix Gamma[`j'+1,1] = `j'/($NUM/20)
}


drop planner_welfare weight temp 

putexcel set ${output}/planner_welfare.xlsx, replace
putexcel A1 = matrix(Gamma)
putexcel B1 = matrix(res)

* save as csv
clear
import excel ${output}/planner_welfare.xlsx, sheet("Sheet1") firstrow clear
export delimited ${output}/planner_welfare.csv, replace
erase ${output}/planner_welfare.xlsx



** histograms of welfare effects
u $data/distribution_${time}_${lastyear}, replace


preserve
twoway__histogram_gen welfare_total if welfare_total > -297.5 & welfare_total < 297.5, start(-297.5) w(5) fraction gen(y x) 
keep x y
keep if y != .
outsheet x y using ${output}/hist_welfare_total_${time}.csv, c replace
restore

preserve
twoway__histogram_gen welfare_total_sc if welfare_total_sc >= -60.5 & welfare_total_sc <= 60.5, fraction start(-60.5) w(1) gen(y x) 
keep x y
keep if y != .
outsheet x y using ${output}/hist_welfare_total_sc_${time}.csv, c replace
restore	
	

preserve
twoway__histogram_gen revgain_total if revgain_total > -297.5 & revgain_total < 697.5, start(-297.5) w(5) fraction gen(y x) 
keep x y
keep if y != .
outsheet x y using ${output}/hist_revgain_total_${time}.csv, c replace
restore
	
preserve
twoway__histogram_gen revgain_total_sc if revgain_total_sc >= -60.5 & revgain_total_sc <= 65.5, fraction start(-60.5) w(1) gen(y x) 
keep x y
keep if y != .
outsheet x y using ${output}/hist_revgain_total_sc_${time}.csv, c replace
restore



u $data/distribution_${time}_${lastyear}, replace
sort welfare_total

preserve
drop if welfare_total == .
sort welfare_total
cap drop percentile
cap drop *_mean
gen percentile = floor(((_n-1)/_N)*100)
foreach var in total housing equity debt deposits {
	bys percentile: egen welfare_`var'_mean = mean(welfare_`var')
	bys percentile: egen welfare_`var'_share_mean = mean(welfare_`var'/welfare_total)
}
bys percentile: keep if _n == 1
outsheet percentile *_mean using $output/welfare_effect_decompose_100.csv, c replace
restore




* rank-rank plots
u $data/distribution_${time}_${lastyear}, replace

sort welfare_total_sc
gen welfare_rank = _n/_N

sort revgain_total_sc
gen revgain_rank = _n/_N

corr welfare_rank revgain_rank

file open fh using "$output/corr_welfare_revgain_sc_rank_${time}.csv", replace write
file write fh "`r(rho)'"
file close fh


preserve 
local num = 5
gen welfare_rank_`num' = ceil((welfare_rank)*`num')
gen revgain_rank_`num' = ceil((revgain_rank)*`num')

bys welfare_rank_`num' revgain_rank_`num': gen fraction = _N
replace fraction = fraction/_N
bys welfare_rank_`num' revgain_rank_`num': keep if _n == 1

outsheet welfare_rank_`num' revgain_rank_`num' fraction using $output/welfare_revgain_sc_heatmap_`num'_${time}.csv, c replace

restore









